<template>
  <!-- 登录注册弹框 -->
  <el-dialog title="请先进行登录！" :visible.sync="dialogVisiable" width="30%">
    <el-row type="flex" justify="center" align="middle" class="login_main">
      <div class="form-wrapper">
        <!-- 表单头部tab -->
        <el-row type="flex" justify="center" class="tabs">
          <span
            :class="{active: currentTab === index}"
            v-for="(item, index) in [`登录`, `注册`]"
            :key="index"
            @click="handleChangeTab(index)"
          >{{item}}</span>
        </el-row>

        <!-- 登录功能组件 -->
        <LoginForm v-if="currentTab == 0" />

        <!-- 注册功能组件 -->
        <RegisterForm @toLogin="currentTab = 0" v-if="currentTab == 1" />
      </div>
    </el-row>
  </el-dialog>
</template>

<script>
import LoginForm from "@/components/user/LoginForm";
import RegisterForm from "@/components/user/RegisterForm";

export default {
  data() {
    return {
      // 登录、注册切换
      currentTab: 0,
    };
  },

  // 对话框显示隐藏
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },

  computed: {
    dialogVisiable: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      },
    },
  },

  components: {
    LoginForm,
    RegisterForm,
  },

  methods: {
    // 登录、注册表单切换
    handleChangeTab(index) {
      this.currentTab = index;
    },
  },
};
</script>

<style lang="less" scoped>
.login_main {
  height: 100%;
  margin: 0 auto;
  position: relative;

  .form-wrapper {
    width: 400px;
    margin: 0 auto;
    background: #fff;
    border: 1px solid #ddd;
    border-top: none;
    overflow: hidden;

    .tabs {
      span {
        display: block;
        width: 50%;
        height: 50px;
        box-sizing: border-box;
        border-top: 2px #eee solid;
        background: #eee;
        line-height: 48px;
        text-align: center;
        cursor: pointer;
        color: #666;

        &.active {
          color: orange;
          border-top-color: orange;
          background: #fff;
          font-weight: bold;
        }
      }
    }
  }
}
</style>